var seckill = {
    URL: {
        now: "/seckill/time/now",
        exposer: function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        execution: function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },
    //秒杀逻辑处理
    handleKill: function (seckillId, node) {
        node.hide().html('<button type="button" id="kill" class="layui-btn layui-btn-danger">开始秒杀</button>');//隐藏计时
        var exposerUrl = seckill.URL.exposer(seckillId);
        $.post(exposerUrl, {}, function (result) {
            if (result && result.success) {
                var exposer = result.data;
                if (exposer.exposed) {
                    node.show();
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId, md5);
                    //绑定一次操作 防止重复点击
                    $("#kill").one('click', function () {
                        //禁用按钮
                        $(this).addClass('layui-btn-disabled');

                        //发送秒杀请求
                        $.post(killUrl, {}, function (result) {
                            if (result && result.success) {
                                var killResult = result.data;
                                var state = killResult.state;
                                var stateInfo = killResult['stateInfo'];
                                node.html('<span class="layui-btn-normal">' + stateInfo + '</span>');
                            }
                        })
                    })
                } else { //秒杀未开始 重新进行秒杀计时
                    var nowTime = exposer.now;
                    var startTime = exposer.startTime;
                    var endTime = exposer.endTime;
                    seckill.countDown(seckillId, nowTime, startTime, endTime);
                }
            } else {
                console.log('result:', result);
            }
        })
    },
    countDown: function (seckillId, nowTime, startTime, endTime) {
        var timeCount = $("#timeCount");
        if (nowTime > endTime) {//秒杀结束
            timeCount.html("秒杀结束");
        } else if (nowTime < startTime) { //秒杀未开始 绑定计时事件
            var killTime = new Date(parseInt(startTime));
            timeCount.countdown(killTime, function (event) {
                var format = event.strftime('秒杀倒计时：%D天 %H时 %M分 %S秒');
                timeCount.html(format).on("finish.countdown", function () {
                    seckill.handleKill(seckillId, timeCount);
                });
            })
        } else {//可执行秒杀
            seckill.handleKill(seckillId, timeCount);
        }
    },
    detail: {
        init: function (params) {
            var startTime = params.startTime;
            var endTime = params.endTime;
            var seckillId = params.seckillId;
            /*获取当前时间 看秒杀是否开始*/
            $.get(seckill.URL.now, {}, function (result) {
                if (result && result.success) {
                    var nowTime = result.data;
                    //计时交互
                    seckill.countDown(seckillId, nowTime, startTime, endTime);
                } else {
                    console.log("result:" + result);
                }

            })
        }
    }
}